﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tester
{
    //委托是一个类，可以定义方法的类型，使该方法可以以参数形式传递给另一个方法，这种将方法动态地赋给参数的做法，可以避免在程序中大量使用if-else（swich），使程序有良好的扩展性。
    public delegate void SayHello(string name);

    public delegate string SetName(string name);

    class Program
    {
        public static SayHello SayHello;
        public static SetName SetName;
        static void Main(string[] args)
        {
            SetName += SetSomeBodyName;
            SayHello += Say;
            StartSet("15");
            StartSay("jueye");
            SayHelloToSomeBody("jueye", SayEnglish);
            Program program = new Program();
            SayHelloToSomeBody("符永丰", program.SayChina);
            Console.ReadKey();
        }
        public static void SayHelloToSomeBody(string name, SayHello sayHello)
        {
            sayHello(name);
        }
        public static string SetSomeBodyName(string name)
        {
            Console.WriteLine(name);
            return name;
        }

        public static  void SayEnglish(string name)
        {
            Console.WriteLine("Hello"+ name);
        }
        public void SayChina(string name)
        {
            Console.WriteLine("你好"+ name);
        }
        public static void StartSay(string name)
        {
            var handler = SayHello;
            if (handler != null)
                SayHello(name);
        }
        public static void StartSet(string name)
        {
            var handler = SetName;
            if (handler != null)
                SetSomeBodyName(name);
        }

        public static void Say(string name)
        {
            Console.WriteLine("Hello," + name);
        }
    }
}
